home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / libg_261.zip / libg_261 / libio / filebuf.cc < prev    next >
C/C++ Source or Header  |  1994-07-11  |  5KB  |  200 lines

  1. /* This is part of libio/iostream, providing -*- C++ -*- input/output.
  2. Copyright (C) 1993 Free Software Foundation
  3.  
  4. This file is part of the GNU IO Library.  This library is free
  5. software; you can redistribute it and/or modify it under the
  6. terms of the GNU General Public License as published by the
  7. Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. This library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU CC; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19. As a special exception, if you link this library with files
  20. compiled with a GNU compiler to produce an executable, this does not cause
  21. the resulting executable to be covered by the GNU General Public License.
  22. This exception does not however invalidate any other reasons why
  23. the executable file might be covered by the GNU General Public License.
  24.  
  25. Written by Per Bothner (bothner@cygnus.com). */
  26.  
  27. #include "iostreamP.h"
  28. #include <sys/types.h>
  29. #include <sys/stat.h>
  30. #include <fcntl.h>
  31. #include <errno.h>
  32. #include "builtinbuf.h"
  33.  
  34. void filebuf::init()
  35. {
  36.   _IO_file_init(this);
  37. }
  38.  
  39. filebuf::filebuf()
  40. {
  41.   _IO_file_init(this);
  42. }
  43.  
  44. /* This is like "new filebuf()", but it uses the _IO_file_jump jumptable,
  45.    for eficiency. */
  46.  
  47. filebuf* filebuf::__new()
  48. {
  49.   filebuf *fb = new filebuf;
  50.   fb->_jumps = &_IO_file_jumps;
  51.   fb->_vtable() = builtinbuf_vtable;
  52.   return fb;
  53. }
  54.  
  55. filebuf::filebuf(int fd)
  56. {
  57.   _IO_file_init(this);
  58.   _IO_file_attach(this, fd);
  59. }
  60.  
  61. filebuf::filebuf(int fd, char* p, int len)
  62. {
  63.   _IO_file_init(this);
  64.   _IO_file_attach(this, fd);
  65.   setbuf(p, len);
  66. }
  67.  
  68. filebuf::~filebuf()
  69. {
  70.     if (!(xflags() & _IO_DELETE_DONT_CLOSE))
  71.     close();
  72.  
  73.     _un_link();
  74. }
  75.  
  76. filebuf* filebuf::open(const char *filename, ios::openmode mode, int prot)
  77. {
  78.   if (_IO_file_is_open (this))
  79.     return NULL;
  80.   int posix_mode;
  81.   int read_write;
  82.   if (mode & ios::app)
  83.     mode |= ios::out;
  84.   if ((mode & (ios::in|ios::out)) == (ios::in|ios::out)) {
  85.     posix_mode = O_RDWR;
  86.     read_write = 0;
  87.   }
  88.   else if (mode & ios::out)
  89.     posix_mode = O_WRONLY, read_write = _IO_NO_READS;
  90.   else if (mode & (int)ios::in)
  91.     posix_mode = O_RDONLY, read_write = _IO_NO_WRITES;
  92.   else
  93.     posix_mode = 0, read_write = _IO_NO_READS+_IO_NO_WRITES;
  94.   if ((mode & (int)ios::trunc) || mode == (int)ios::out)
  95.     posix_mode |= O_TRUNC;
  96.   if (mode & ios::app)
  97.     posix_mode |= O_APPEND, read_write |= _IO_IS_APPENDING;
  98.   if (!(mode & (int)ios::nocreate) && mode != ios::in)
  99.     posix_mode |= O_CREAT;
  100.   if (mode & (int)ios::noreplace)
  101.     posix_mode |= O_EXCL;
  102.   int fd = ::open(filename, posix_mode, prot);
  103.   if (fd < 0)
  104.     return NULL;
  105.   _fileno = fd;
  106.   xsetflags(read_write, _IO_NO_READS+_IO_NO_WRITES+_IO_IS_APPENDING);
  107.   if (mode & (ios::ate|ios::app)) {
  108.     if (sseekoff(0, ios::end) == EOF)
  109.       return NULL;
  110.   }
  111.   _IO_link_in(this);
  112.   return this;
  113. }
  114.  
  115. filebuf* filebuf::open(const char *filename, const char *mode)
  116. {
  117.   return (filebuf*)_IO_file_fopen(this, filename, mode);
  118. }
  119.  
  120. filebuf* filebuf::attach(int fd)
  121. {
  122.   return (filebuf*)_IO_file_attach(this, fd);
  123. }
  124.  
  125. streambuf* filebuf::setbuf(char* p, int len)
  126. {
  127.   return _IO_file_setbuf(this, p, len) == 0 ? this : NULL;
  128. }
  129.  
  130. int filebuf::doallocate() { return _IO_file_doallocate(this); }
  131.  
  132. int filebuf::overflow(int c)
  133. {
  134.   return _IO_file_overflow(this, c);
  135. }
  136.  
  137. int filebuf::underflow()
  138. {
  139.   return _IO_file_underflow(this);
  140. }
  141.  
  142. int filebuf::do_write(const char *data, int to_do)
  143. {
  144.   return _IO_do_write(this, data, to_do);
  145. }
  146.  
  147. int filebuf::sync()
  148. {
  149.   return _IO_file_sync(this);
  150. }
  151.  
  152. streampos filebuf::seekoff(streamoff offset, _seek_dir dir, int mode)
  153. {
  154.   return _IO_file_seekoff (this, offset, convert_to_seekflags(dir, mode));
  155. }
  156.  
  157. filebuf* filebuf::close()
  158. {
  159.   return (_IO_file_close_it(this) ? NULL : this);
  160. }
  161.  
  162. streamsize filebuf::sys_read(char* buf, streamsize size)
  163. {
  164.   return _IO_file_read(this, buf, size);
  165. }
  166.  
  167. streampos filebuf::sys_seek(streamoff offset, _seek_dir dir)
  168. {
  169.   return _IO_file_seek(this, offset, dir);
  170. }
  171.  
  172. streamsize filebuf::sys_write(const char *buf, streamsize n)
  173. {
  174.   return _IO_file_write (this, buf, n);
  175. }
  176.  
  177. int filebuf::sys_stat(void* st)
  178. {
  179.   return _IO_file_stat (this, st);
  180. }
  181.  
  182. int filebuf::sys_close()
  183. {
  184.   return _IO_file_close (this);
  185. }
  186.  
  187. streamsize filebuf::xsputn(const char *s, streamsize n)
  188. {
  189.   return _IO_file_xsputn(this, s, n);
  190. }
  191.  
  192. streamsize filebuf::xsgetn(char *s, streamsize n)
  193. {
  194.     // FIXME: OPTIMIZE THIS (specifically, when unbuffered()).
  195.     return streambuf::xsgetn(s, n);
  196. }
  197.  
  198. // Non-ANSI AT&T-ism:  Default open protection.
  199. const int filebuf::openprot = 0644;
  200.